import tools from '@/tools'

/**
 * @description 坐标校验
 * @author 赵致喜
 * @date 2020-04-14 13:28:27
 * @version V1.0.0
 */

/**
 * @functionName coordinateValidation
 * @param {Number} east 被检测数据（东经）
 * @param {Number} north 被检测数据（北纬）
 * @param {String} area 被检测数据地区（中国为空）
 * @return {Object} 返回结果
 * @description 校验坐标是否在中国范围内
 * @author 赵致喜
 * @date 2020-04-14 13:29:29
 * @version V1.0.0
 */

export const coordinateValidation = (east = 0, north = 0, area = '') => {
  const coordinateType = isCoordinate(east, north, area)
  const EastType = isEast(east, area)
  const northType = isNorth(north, area)
  if (!coordinateType.result) return coordinateType
  if (!EastType.result) return EastType
  if (!northType.result) return northType
  if (EastType.result && northType.result) {
    coordinateType.result = true
    coordinateType.message = '校验成功！'
    return coordinateType
  }
}

/**
 * @functionName isCoordinate
 * @param {Number} east 被检测数据（东经）
 * @param {Number} north 被检测数据（北纬）
 * @param {String} area 被检测数据地区
 * @return {Object} 返回结果
 * @description 校验参数类型是否符合
 * @author 赵致喜
 * @date 2020-04-14 13:51:14
 * @version V1.0.0
 */

const isCoordinate = (east, north, area) => {
  const eastType = tools.getRawType(east)
  const northType = tools.getRawType(north)
  const areaType = tools.getRawType(area)
  const resultObj = tools.resultObj(
    { east, north, area },
    false,
    '参数"east"或"north"类型不正确，请传入[Number]类型！'
  )
  if (eastType === 'Number' && northType === 'Number') {
    if (areaType === 'String') {
      resultObj.result = true
      resultObj.message = '校验成功！'
      return resultObj
    } else {
      resultObj.message = '参数"area"类型不正确，请传入[String]类型！'
      return resultObj
    }
  }
  return resultObj
}

/**
 * @functionName isCoordinate
 * @param {Number} east 被检测数据（东经）
 * @param {String} area 被检测地区
 * @return {Object} 返回结果
 * @description 校验东经坐标
 * @author 赵致喜
 * @date 2020-04-14 13:51:14
 * @version V1.0.0
 */

const isEast = (east, area) => {
  const eastMax = 135.04166666666666
  const eastMin = 73.66666666666667
  const eastTJMax = 117.82
  const eastTJMin = 116.92
  const resultObj = tools.resultObj(
    { east },
    false,
    `参数"east"不在有效坐标内，请传入“${eastMin} ~ ${eastMax} ”区间内坐标！`
  )

  if (area === 'tianjin') {
    if (east >= eastTJMin && east <= eastTJMax) {
      resultObj.result = true
      resultObj.message = '校验成功！'
      return resultObj
    } else {
      resultObj.message = `参数"east"不在有效坐标内，请传入“${eastTJMin} ~ ${eastTJMax} ”区间内坐标！`
      return resultObj
    }
  } else if (east >= eastMin && east <= eastMax) {
    resultObj.result = true
    resultObj.message = '校验成功！'
    return resultObj
  }
  return resultObj
}

/**
 * @functionName isNorth
 * @param {Number} north 被检测数据（北纬）
 * @param {String} area 被检测地区
 * @return {Object} 返回结果
 * @description 校验北纬坐标
 * @author 赵致喜
 * @date 2020-04-14 14:22:14
 * @version V1.0.0
 */

const isNorth = (north, area) => {
  const northMax = 53.55
  const northMin = 3.8666666666666667
  const northTJMax = 40.05
  const northTJMin = 38.83
  const resultObj = tools.resultObj(
    { north },
    false,
    `参数"north"不在有效坐标内，请传入“${northMin} ~ ${northMax} ”区间内坐标！`
  )

  if (area === 'tianjin') {
    if (north >= northTJMin && north <= northTJMax) {
      resultObj.result = true
      resultObj.message = '校验成功！'
      return resultObj
    } else {
      resultObj.message = `参数"north"不在有效坐标内，请传入“${northTJMin} ~ ${northTJMax} ”区间内坐标！`
      return resultObj
    }
  } else if (north >= northMin && north <= northMax) {
    resultObj.result = true
    resultObj.message = '校验成功！'
    return resultObj
  }
  return resultObj
} 
